﻿' Developer Express Code Central Example:
' How to customize the default Find Panel filtering algorithm
' 
' Find Panel uses an extended syntax that allows you to specify complex criteria.
' You can find detailed information here:
' - Find Panel
' (ms-help://DevExpress.NETv10.2/DevExpress.WindowsForms/CustomDocument8869.htm)
' 
' However,
' sometimes it is necessary to exclude/include some rows in the resulting list
' according to your custom logic. In this case, you can filter rows manually by
' handling View.CustomRowFilter Event
' (ms-help://DevExpress.NETv10.2/DevExpress.WindowsForms/DevExpressXtraGridViewsBaseColumnView_CustomRowFiltertopic.htm).
' This example demonstrates how to construct your own filter criteria and use
' ExpressionEvaluator to check whether a row should be hidden. For demo purposes,
' this example just shows how to find exact matches of a typed string. The same
' result can be accomplished with the default behavior if you enclose your text in
' commas in Find Panel. To provide your own logic, just modify the
' MyConvertFindPanelTextToCriteriaOperator method as your needs dictate.
' 
' You can find sample updates and versions for different programming languages here:
' http://www.devexpress.com/example=E2801

Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Imports DevExpress.XtraGrid.Views.Grid
Imports DevExpress.Data.Filtering
Imports DevExpress.Data.Helpers
Imports DevExpress.XtraGrid.Columns
Imports DevExpress.Data
Imports DevExpress.XtraEditors.Repository
Imports DevExpress.XtraGrid.Views.Base
Imports DevExpress.Data.Filtering.Helpers
Imports System.Collections

Namespace IControls
    Public NotInheritable Class FilterCriteriaHelper

        Private Sub New()
        End Sub
        Public Shared Function ReplaceFilterCriteria(ByVal source As CriteriaOperator, ByVal prevOperand As CriteriaOperator, ByVal newOperand As CriteriaOperator) As CriteriaOperator
            Dim groupOperand As GroupOperator = TryCast(source, GroupOperator)
            If ReferenceEquals(groupOperand, Nothing) Then
                Return newOperand
            End If
            Dim clone As GroupOperator = groupOperand.Clone()
            clone.Operands.Remove(prevOperand)
            If clone.Equals(source) Then
                Return newOperand
            End If
            clone.Operands.Add(newOperand)
            Return clone
        End Function

        Public Shared Function ReplaceFindPanelCriteria(ByVal source As CriteriaOperator, ByVal view As IGridView, ByVal newOperand As CriteriaOperator) As CriteriaOperator
            Return ReplaceFilterCriteria(source, ConvertFindPanelTextToCriteriaOperator(view.FindFilterText, view, True), newOperand)
        End Function

        Public Shared Function ConvertFindPanelTextToCriteriaOperator(ByVal findPanelText As String, ByVal view As GridView, ByVal applyPrefixes As Boolean) As CriteriaOperator
            If (Not String.IsNullOrEmpty(findPanelText)) Then
                Dim parseResult As FindSearchParserResults = New FindSearchParser().Parse(findPanelText, GetFindToColumnsCollection(view))
                If applyPrefixes Then
                    parseResult.AppendColumnFieldPrefixes()
                End If

                Return DxFtsContainsHelperAlt.Create(parseResult, FilterCondition.Contains, False)
            End If
            Return Nothing
        End Function
        Private Shared Function GetFindToColumnsCollection(ByVal view As IGridView) As ICollection
            Dim mi As System.Reflection.MethodInfo = GetType(ColumnView).GetMethod("GetFindToColumnsCollection", System.Reflection.BindingFlags.NonPublic Or System.Reflection.BindingFlags.Instance)
            Return TryCast(mi.Invoke(view, Nothing), ICollection)
        End Function

        Public Shared Function MyConvertFindPanelTextToCriteriaOperator(ByVal view As IGridView) As CriteriaOperator
            Return ConvertFindPanelTextToCriteriaOperator(String.Format("""{0}""", """"c & view.FindFilterText & """"c), view, True)
        End Function
    End Class
End Namespace